package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.to.PurchaseCountTo;
import com.atguigu.jxc.to.PurchaseTo;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:PurchaseListGoodsServiceImpl
 * @Version: 1.0
 * @CreateAt:2021/3/8 10:30
 * @Author: ✨HanSan·胡✨
 * @Description:
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {
    @Autowired
    PurchaseListDao purchaseListDao;

    @Transactional
    @Override
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        purchaseList.setUserId(user.getUserId());
        purchaseListDao.save(purchaseList);
        Gson gson = new Gson();
        List<PurchaseListGoods> list = gson.fromJson(purchaseListGoodsStr,new TypeToken<List<PurchaseListGoods>>(){}.getType());
        for (PurchaseListGoods purchaseListGoods : list) {
            purchaseListGoods.setPurchaseListId(purchaseList.getPurchaseListId());
        }
        if (list.size()>0){
            purchaseListDao.savePurchaseListGoods(list);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map listByPage(PurchaseTo purchaseTo) {
        if(null == purchaseTo.getPageSize() ||purchaseTo.getPageSize()<=0 ){
            purchaseTo.setPageSize(0);
        }else {
            purchaseTo.setPageSize(purchaseTo.getPageSize()-1);
        }

        if(null == purchaseTo.getPageCount() ||purchaseTo.getPageCount()<20 ){
            purchaseTo.setPageCount(20);
        }
        Map map = new HashMap();
        List<Map> list = purchaseListDao.listByPage(purchaseTo);
        map.put("rows",list);
        map.put("page_now",purchaseTo.getPageSize());
        map.put("size_now",purchaseTo.getPageCount());
        return map;
    }

    @Override
    public Map goodsList(Integer purchaseListId) {
        Map map = new HashMap();
        List<PurchaseListGoods> purchaseListGoods = purchaseListDao.goodsList(purchaseListId);
        map.put("rows",purchaseListGoods);
        return map;
    }

    @Transactional
    @Override
    public ServiceVO delete(Integer purchaseListId) {
        purchaseListDao.deleteByPurchaseListGoods(purchaseListId);
        purchaseListDao.deleteByPurchaseList(purchaseListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO updateState(Integer purchaseListId) {
        purchaseListDao.updateState(purchaseListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<Map> count(PurchaseCountTo purchaseCountTo) {
        String codeOrName = purchaseCountTo.getCodeOrName();
        if(null!=codeOrName && !"".equals(codeOrName)){
            try {
                int i = Integer.parseInt(codeOrName);
                purchaseCountTo.setStatus(1);
            }catch (Exception e){
                purchaseCountTo.setStatus(2);
            }
        }

        List<Map> list = purchaseListDao.count(purchaseCountTo);
        return list;
    }
}
